package top.oylan.manager.tools;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2018/12/23.
 */
@Component
public class FileUploader {

    @Value("${systemConfig.uploadPath}")
    private String rootPath;

    public Result upload(MultipartFile file,Directory dir){

        StringBuffer sb = new StringBuffer();
        Calendar calendar = Calendar.getInstance();
        String year = String.valueOf(calendar.get(Calendar.YEAR));
        String  month = String.valueOf(calendar.get(Calendar.MONTH)+1);

        switch (dir){
            case ART:
                sb.append("art/");
                sb.append(year);
                sb.append("/");
                sb.append(month);
                sb.append("/");
                break;
            case IMAGE:
                sb.append("image/");
                sb.append(year);
                sb.append("/");
                sb.append(month);
                sb.append("/");
                sb.append(calendar.get(Calendar.DAY_OF_MONTH));
                sb.append("/");
                break;
            case SYSTEM:
                sb.append("system/");
                sb.append(year);
                sb.append("/");
                break;
            case CATEGORY:
                sb.append("category/");
                sb.append(year);
                sb.append("/");
                break;
            case USER:
                sb.append("user/");
                sb.append(year);
                sb.append("/");
                break;
            case BOOK:
                sb.append("book/");
                sb.append(year);
                sb.append("/");
                break;
            default:
                sb.append("system/");
                sb.append(year);
                sb.append("/");
                sb.append(month);
                sb.append("/");
                break;
        }

        return doUpload(file,sb.toString());
    }

    public Result upload(MultipartFile file,String path){

      return doUpload(file,path);
    }

    public Result doUpload(MultipartFile file, String targetFilePath){

        Result result = new Result();
        if(file != null && !file.isEmpty()){
            String fileName = file.getOriginalFilename();

            String type = fileName.substring(fileName.lastIndexOf("."),fileName.length());
            long time = System.currentTimeMillis();
            String saveFile = targetFilePath+String.valueOf(time)+type;
            File targetFile = new File(rootPath+saveFile);

            File directory = new File(rootPath+targetFilePath);

            if(!directory.isDirectory()){
                directory.mkdirs();
            }

            try {
                file.transferTo(targetFile);
                targetFile.setExecutable(true);
                targetFile.setReadable(true);
                targetFile.setWritable(true);
                result.setCode((byte) 1);
                result.setMessage("上传成功");
                Map<String,Object> map = new HashMap<String ,Object>();
                map.put("path","/"+saveFile);
                result.setData(map);
                targetFile.setReadable(true);
                targetFile.setWritable(true);
            } catch (IOException e) {
                e.printStackTrace();
                result.setCode((byte)-1);
                result.setMessage(e.getMessage());
            }
        }else{
            result.setCode((byte) 2);
            result.setMessage("请上传图片");
        }
        return result;
    }

    public void removeFile(String path) {
        if(path !=null && !"".equals(path)){
            File file = new File(path);
            if(file.exists()){
                file.delete();
            }
        }
    }
}
